package com.example.minefieldkeepercloud.activity.machinerymaintain.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.activity.machinerymaintain.MachineDetailAc;
import com.example.minefieldkeepercloud.adapter.ListFmAdapter;
import com.example.minefieldkeepercloud.bean.ChartFragBean;
import com.example.minefieldkeepercloud.bean.FactoryBean;
import com.example.minefieldkeepercloud.bean.MachineDetailBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.view.lazyviewpager.LazyFragmentPagerAdapter;
import com.example.minefieldkeepercloud.view.pop.FactorySelectPop;
import com.github.mikephil.charting.charts.BarChart;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChartFragment extends Fragment implements LazyFragmentPagerAdapter.Laziable {
    @BindView(R.id.Bar_chat)
    BarChart barChart2;
    @BindView(R.id.ly_top)
    LinearLayout relative;
    @BindView(R.id.tv_FactoryName)
    TextView tv_FactoryName;
    @BindView(R.id.tv_FullName)
    TextView tv_FullName;

    private FactorySelectPop factorySelectPop;
    private TimePickerView pvTimeDay;//年月日时间选择器
    private String NowData="";
    private SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM");
    private String FactoryId="";
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_chart, container, false);
        ButterKnife.bind(this, view);
        initview();
        getMineField();
        return view;
    }
    /**
     * 初始化
     */
    private void initview() {

        //时间选择器
        pvTimeDay = new TimePickerView(getContext(), TimePickerView.Type.YEAR_MONTH);
        //设置范围
        pvTimeDay.setRange(2017, 2030);

        //设置是否循环滚动
        pvTimeDay.setCyclic(false);
        pvTimeDay.setCancelable(true);

        //时间选择后回调
        pvTimeDay.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                NowData=ymd.format(date);
                tv_FullName.setText(NowData);


            }


        });
        NowData = ymd.format(new Date(System.currentTimeMillis()));
        tv_FullName.setText(NowData);

    }

    private void getMineField() {
        if (!NetWorkUtil.check(getContext())) {
            return;
        }
        NetHelperNew.GetFactory(new ApiHttpCallback(getContext(), 1,true) {
            @Override
            public void onSuccess(String data) {
                FactoryBean factoryBean = MyGson.getInstance().fromJson(data, FactoryBean.class);
                if (factoryBean.getType() == 1) {
                    if (factoryBean.getData().size() > 0) {
                        CreatePop(factoryBean);
                    } else {
                        ToastUtil.showToast(getContext(), "没有可查询的矿场");
                    }

                } else {
                    ToastUtil.showToast(getContext(), factoryBean.getContent());
                }
            }
        });


    }
    private void CreatePop(FactoryBean dataBean) {
        if(FactoryId.isEmpty())
        {
            FactoryId=dataBean.getData().get(0).getID()+ "";
        }
        if (factorySelectPop == null) {
            factorySelectPop = new FactorySelectPop(getContext(), dataBean, new FactorySelectPop.OnSelectListener() {
                @Override
                public void select(int id, String name) {
                    FactoryId=id+"";
                    tv_FactoryName.setText(name);

                }
            });
        }

        tv_FactoryName.setText(dataBean.getData().get(0).getFactoryName());
        getNetData();
    }


    private void getNetData()
    {
        if (!NetWorkUtil.check(getContext())) {
            return;
        }
        NetHelperNew.GetErrInfoStatistics_Chart(FactoryId, NowData, new ApiHttpCallback(getActivity(),1,true) {
            @Override
            public void onSuccess(String data) {
                ChartFragBean chartFragBean = MyGson.getInstance().fromJson(data, ChartFragBean.class);
                if (chartFragBean.getType() == 1) {
                    if (chartFragBean.getData().size() > 0) {
                        showBarChartMore(chartFragBean);
                    } else {
                        ToastUtil.showToast(getActivity(), "没有相关数据");
                    }

                } else {
                    ToastUtil.showToast(getActivity(), chartFragBean.getContent());
                }
            }
        });
    }

    @OnClick({R.id.ly_user,R.id.ly_Minefield,R.id.search})
    public void onClick(View view) {
        switch (view.getId()) {


            case R.id.ly_user:
                pvTimeDay.setTime(new Date(System.currentTimeMillis()));
                pvTimeDay.show();
                break;
            case R.id.ly_Minefield:
                if (factorySelectPop != null)
                    factorySelectPop.showAsDropDown(relative, 0, 0);
                else
                    ToastUtil.showToast(getContext(), "没有可查询的矿场");
                break;
            case R.id.search:
                if (!NetWorkUtil.check(getContext())) {
                    return;
                }


                getNetData();
                break;
        }
    }
    private void showBarChartMore(ChartFragBean chartFragBean) {

        BarChartManager barChartManager = new BarChartManager(barChart2);
        List<Float> xAxisValues = new ArrayList<>();
        List<List<Float>> yAxisValues = new ArrayList<>();
        List<String> labels = new ArrayList<>();
        List<Integer> colours = new ArrayList<>();
        List<Float> x1 = new ArrayList<>();
        List<Float> x2 = new ArrayList<>();
        List<Float> x3 = new ArrayList<>();
        List<String> names = new ArrayList<>();
        for (int i=0;i<chartFragBean.getData().size();i++)
        {
            xAxisValues.add(Float.valueOf(i+1));
            x1.add(Float.valueOf(chartFragBean.getData().get(i).getHl()));
            x2.add(Float.valueOf(chartFragBean.getData().get(i).getWx()));
            x3.add(Float.valueOf(chartFragBean.getData().get(i).getWh()));
            names.add(chartFragBean.getData().get(i).getHandleUserName());
        }

//        xAxisValues.add(1.0f);
//        xAxisValues.add(2.0f);
//        xAxisValues.add(3.0f);
//        xAxisValues.add(4.0f);
//        xAxisValues.add(5.0f);
//        xAxisValues.add(6.0f);
//        xAxisValues.add(7.0f);
//        xAxisValues.add(8.0f);
//        xAxisValues.add(9.0f);

//        x1.add(10f);
//        x1.add(20f);
//        x1.add(30f);
//        x1.add(40f);
//        x1.add(50f);
//        x1.add(50f);
//        x1.add(40f);
//        x1.add(50f);
//        x1.add(50f);
//
//        x2.add(50f);
//        x2.add(40f);
//        x2.add(30f);
//        x2.add(20f);
//        x2.add(10f);
//        x2.add(10f);
//        x2.add(20f);
//        x2.add(10f);
//        x2.add(10f);
//
//        x3.add(50f);
//        x3.add(40f);
//        x3.add(30f);
//        x3.add(20f);
//        x3.add(10f);
//        x3.add(10f);
//        x3.add(20f);
//        x3.add(10f);
//        x3.add(10f);

        yAxisValues.add(x1);
        yAxisValues.add(x2);
        yAxisValues.add(x3);
        labels.add("");
        labels.add("");
        labels.add("");
        colours.add(Color.parseColor("#C23531"));
        colours.add(Color.parseColor("#2F4554"));
        colours.add(Color.parseColor("#61A0A8"));
        barChartManager.showMoreBarChart(chartFragBean,xAxisValues, yAxisValues, labels, colours,names);
        barChartManager.setXAxis(chartFragBean.getData().size(), 0, chartFragBean.getData().size());
    }



}
